package com.team2.shoppingService;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.team2.shared.ServerProduct;

public class GoogleShoppingRequest extends ShoppingRequest{


	final static String API_URL = "https://www.googleapis.com/shopping/search/v1/public/products?";
	
	
	protected String key = "AIzaSyCVP6qxzIx7TAQDJJ-5zNootEcrL3fOkQE";
	protected boolean isSpellChecking = true;
	public GoogleShoppingRequest(String tags) {
		super();
		this.tags = tags;
	}
	public ArrayList<ServerProduct> execute(){
		String json = Util.getRawHttpResponse(this.toUrlString());
		return parseJsonToProducts(json);
	}
	public String toUrlString(){
		String url = "";
		url += API_URL+"key="+key+"&country="+country;
		String boolString = "";
		if (isSpellChecking){
			boolString = "true";
		}else{
			boolString = "false";
		}
		url += "&spelling.enabled="+boolString;
		
		url += "&"+getQueryString();
		url +="&restrictBy=brand%3A"+this.brand;
		url += "&rankBy="+this.rankby+"%3A"+this.order;
		url += "&"+this.alt;
		return url;
	}
	public static ArrayList<ServerProduct> parseJsonToProducts (String json){
		ArrayList<ServerProduct> products = new ArrayList<ServerProduct> ();
		try {
			JSONObject root = new JSONObject(json);
			JSONArray items = root.getJSONArray("items");
			for(int i = 0; i < items.length();i++){
				JSONObject item = items.getJSONObject(i).getJSONObject("product");
				ServerProduct product = new ServerProduct(item.getString("title"), 
											  item.getString("description"), 
											  item.getJSONArray("images").getJSONObject(0).getString("link"));
				product.setBrand(item.getString("brand"));
				product.setCondition(item.getString("condition"));
				product.setLink(item.getString("link"));
				String price = "$"+item.getJSONArray("inventories").getJSONObject(0).getDouble("price");
				product.setPrice(price);			
				products.add(product);
			}
			
			return products;
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return products;
		}
		
	}
	public void addBrand(String brand){
		if(this.brand.equals("")){
			this.brand = brand;
		}
		else{
			this.brand += "%7C"+brand;
		}
	}
	private String getQueryString(){
    	String queryString = "";
    	for (String tag: tags.split(" ")){
    		if (queryString.equals("")){
    			queryString += tag;
    		}
    		else{
    			queryString += "+"+tag;
    		}
    	};
    	return "q=%22"+queryString+"%22";
	}

	
	public String getTags() {
		return tags;
	}


	public void setTags(String tags) {
		this.tags = tags;
	}


	public String getKey() {
		return key;
	}


	public void setKey(String key) {
		this.key = key;
	}


	public String getCountry() {
		return country;
	}


	public void setCountry(String country) {
		this.country = country;
	}

	public void setRankbyPrice(){
		this.rankby = "price";
		this.order = "ascending";
	}
	
	public void setRankbyRelevance(){
		this.rankby = "relevancy";
		this.order = "descending";
	}
	public String getRankby() {
		return rankby;
	}


	public void setRankby(String rankby) {
		this.rankby = rankby;
	}


	public String getBrand() {
		return brand;
	}


	public void setBrand(String brand) {
		this.brand = brand;
	}


	public boolean isSpellChecking() {
		return isSpellChecking;
	}


	public void setSpellChecking(boolean isSpellChecking) {
		this.isSpellChecking = isSpellChecking;
	}


	public String getAlt() {
		return alt;
	}


	public void setAlt(String alt) {
		this.alt = alt;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}
	
}
